#include "webwindow.h"
#include "qwebframe.h"
#include "qwebelement.h"
#include "qdebug.h"

void WebWindow::updateFilePath() {
	if (!this->window->isVisible()) return;
	this->window->setWindowFilePath(this->filePath);
}

bool WebWindow::eventFilter(QObject *o, QEvent *e) {
	/*if (e->type() == QEvent::HoverMove) {

		QMouseEvent* me = (QMouseEvent*)e;
		QPoint cursorPos = this->window->centralWidget()->mapToParent(
			QPoint(me->pos().y(), me->pos().x())
		);

		if (cursorPos.x() < 0 or cursorPos.y() < 0) {
			QVariantList position;
			position.append(me->pos().y());
			position.append(me->pos().x());
			this->fireEvent("mousemove", position);
		}

	} else */if (e->type() == QEvent::WindowStateChange) {
		Qt::WindowStates newState(this->window->windowState());
		Qt::WindowStates oldState(((QWindowStateChangeEvent*)e)->oldState());
		if ((newState ^ oldState) & Qt::WindowMinimized) {
			if (this->window->isMinimized()) this->fireEvent("minimized");
			else this->fireEvent("restored");
		} else if ((newState ^ oldState) & Qt::WindowMaximized) {
			if (this->window->isMaximized()) this->fireEvent("expanded");
			else this->fireEvent("collapsed");
		}
	} else if (e->type() == QEvent::WindowActivate) {
		this->fireEvent("activate");
	} else if (e->type() == QEvent::WindowDeactivate) {
		this->fireEvent("deactivate");
	} else if (e->type() == QEvent::Resize) {
		QVariantList size;
		size.append(this->window->geometry().width());
		size.append(this->window->geometry().height());
		this->fireEvent("resize", size);
	}
	return this->window->eventFilter(o, e);
}

void WebWindow::javaScriptWindowObjectCleared() {
	//this->fireEvent("cleanup", this);
}

bool WebWindow::onCloseQuery(QCloseEvent *event) {
	return this->fireEvent("closequery");
}

WebWindow::WebWindow(QObject *parent): WebHost(parent) {
	this->window = new Window(0);
	this->window->installEventFilter(this);
	this->window->setAttribute(Qt::WA_MacBrushedMetal, true);
	this->webView = new QWebView(this->window);
	this->window->setCentralWidget(this->webView);
	QPalette palette = this->webView->palette();
	palette.setBrush(QPalette::Base, Qt::transparent);
	this->webView->setPalette(palette);

	QObject::connect(this->window, SIGNAL(
		onCloseQuery(QCloseEvent*)
	), this, SLOT(onCloseQuery(QCloseEvent*)));

	/*
	QObject::connect(this->webPage->mainFrame(), SIGNAL(
		javaScriptWindowObjectCleared()
	), this, SLOT(javaScriptWindowObjectCleared()));
	*/
}

WebWindow::~WebWindow() {
	delete this->window;
}

/* exposed declarations */

QString WebWindow::getTitle() {
	return this->window->windowTitle();
}

void WebWindow::setTitle(QString title) {
	this->window->setWindowTitle(title);
}

bool WebWindow::getVisible() {
	return this->window->isVisible();
}

void WebWindow::setVisible(bool visible) {
	this->webView->setPage(visible ? this->webPage : 0);
	this->webView->setAttribute(Qt::WA_OpaquePaintEvent, false);
	this->window->setVisible(visible);
	this->updateFilePath();
}

double WebWindow::getOpacity() {
	return this->window->windowOpacity();
}

void WebWindow::setOpacity(double opacity) {
	if (opacity < 0) opacity = 0;
	if (opacity > 1) opacity = 1;
	this->window->setWindowOpacity(opacity);
}

bool WebWindow::getModified() {
	return this->window->isWindowModified();
}

void WebWindow::setModified(bool modified) {
	this->window->setWindowModified(modified);
}

QString WebWindow::getFilePath() {
	return this->filePath;
}

void WebWindow::setFilePath(QString filePath) {
	this->filePath = filePath;
	this->updateFilePath();
}

void WebWindow::setGeometry(int x, int y, int w, int h) {
	this->window->setGeometry(x, y, w, h);
}

void WebWindow::setWindowState(int windowState) {
	if (windowState == 1) {
		this->window->setWindowState(Qt::WindowMaximized);
	} else if (windowState == 2) {
		this->window->setWindowState(Qt::WindowFullScreen);
	} else {
		this->window->setWindowState(Qt::WindowNoState);
	}
}

void WebWindow::setWindowMinWidth(int minWidth) {
	this->window->setMinimumWidth(minWidth);
}

void WebWindow::setWindowMinHeight(int minHeight) {
	this->window->setMinimumHeight(minHeight);
}

void WebWindow::grabMouse() {
	//qDebug() << this->window->centralWidget();
	//this->window->centralWidget()->grabMouse();
}
